<% title "\"#{@table.name}\" Data"%>

<h2 class='left'>Table: <%= @table.name %></h2>
<div class='right'>
  <%= link_to '+ ADD', table_new_path(@table), remote: true, class: 'button small' %>
</div>

<div class='clear'></div>

<hr/>

<dl class="sub-nav">
  <dd><%= render '/rails_db/shared/exp_col' %></dd>
  <dd class="active"><%= link_to raw("#{fa_icon('table')} Records"), table_data_path(@table), remote: true %></dd>
  <dd><%= link_to raw("#{fa_icon('tasks')} Schema"), table_path(@table), remote: true %></dd>
  <dd><%= select_top_from_table(@table.name) %></dd>
  <dd>
    <a href="#" data-options="align: bottom" data-dropdown="download-files"><%= fa_icon 'download' %> Export</a>
  </dd>
  <dd>
    <a href="#" data-options="align: bottom" data-dropdown="drop"><%= fa_icon 'th-large' %> Functions</a>
  </dd>
  <dd class="right"><%= link_to raw("#{fa_icon('cogs')} Settings"), '#', :class => 'settings-link', :table_name => "#{@table.name}" %></dd>
  <dd class="right"><%= link_to raw("#{fa_icon('search')} Search"), '#', :class => 'search-link' %></dd>
</dl>
<div class="clearfix"></div>

<ul id="drop" class="f-dropdown" data-dropdown-content>
  <li><%= link_to raw("#{fa_icon('trash-o')} Truncate Table"), table_truncate_path(@table), data: {confirm: 'Are you sure?'} %></li>
</ul>

<ul id="download-files" class="f-dropdown" data-dropdown-content>
  <li><%= link_to raw("#{fa_icon('file-text-o')} Export table to CSV"), table_csv_path(@table) %></li>
  <% if defined? Axlsx %>
    <li><%= link_to raw("#{fa_icon('file-excel-o')} Export table to Excel"), table_xlsx_path(@table, format: 'xls') %></li>
  <% end %>
</ul>

<div class='scrollable'>
  <%= search_form_for @q, url: table_data_path(@table), method: :get, remote: true, html: { id: 'search_form', class: search_form_class } do |f| %>
    <div class='panel no_bottom_padding'>
      <h6>Search</h6>
      <%= f.condition_fields do |c| %>
        <%= render "condition_fields", f: c %>
      <% end %>
      <p><%= link_to_add_fields raw("#{fa_icon 'plus'} Add Conditions"), f, :condition %></p>
      <div class="field sorting_options">
        Sort:
        <%= f.sort_fields do |s| %>
          <%= s.sort_select %>
        <% end %>
      </div>
      <div class="actions"><%= f.submit "Search", class: 'button small' %></div>
    </div>
  <% end %>

  <div class='rails-db-settings panel no_bottom_padding' style="display:none">
    <h6>Settings</h6>
    <% @table.columns.each do |column| %>
      <label class='column-name'>
        <%= check_box_tag(column.name, nil, column_is_checked?(@table.name, column.name), class: "#{column.name}") %>
        <%= column.name %>
      </label>
    <% end %>
    <div class='clearfix'></div>
  </div>

  <table id="search_records" class='table'>
    <thead>
      <tr>
        <% if @table.primary_key %>
          <th colspan="2">Actions</th>
        <% end %>
        <% @model.column_names.each do |column| %>
          <th class="column_<%= column %>" style="<%= display_style_column(@table.name, column)%>">
            <%= sort_link @q, column, column, controller: :tables, action: :data %>
          </th>
        <% end %>
      </tr>
    </thead>
    <tbody>
      <% @records.each do |record| %>
        <tr class="row_<%= record[@table.primary_key] %>">
          <%= render 'row', record: record %>
        </tr>
      <% end %>

      <% if @records.none? %>
        <td>No Records</td>
      <% end %>
    </tbody>

    <% if @records.size > 10 %>
      <tfoot>
        <tr>
          <% if @table.primary_key %>
            <th colspan="2">Actions</th>
          <% end %>
          <% @model.column_names.each do |column| %>
            <th> <%= sort_link @q, column, column, controller: :tables, action: :data %> </th>
          <% end %>
        </tr>
      </tfoot>
    <% end %>
  </table>
</div>

<p>
  <span class="radius success label">Total: <%= pluralize @records.total_count, 'record' %></span>
  <span class="radius secondary label"><a href="#" data-reveal-id="sql_modal" id='sql_modal_link'>SQL</a></span>
</p>

<% if @records.total_count > 10 %>
  <div class='left'>
    <%= form_tag table_pagination_path, method: :get, remote: true do %>
      <%= select_tag :per_page, options_for_select([10, 15, 20, 50, 100, 200, 500], per_page), class: 'per_page_pagination_select' %>
    <% end %>
  </div>

  <div class='left per_page_pagination'>
    <%= paginate @records, remote: true, params: { controller: :tables, action: :data } %>
  </div>
<% end %>

<div class='clear'></div>

<%= render 'sql', sql: @sql %>
